/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-09-26
 */
package org.jeecg.modules.flow.callback.outbound.task;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.entity.vo.OutBoundTaskSaveVO;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.callback.outbound.util.CommonOutboundUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.utils.service.IWmsBaseDocumentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 出库任务提交 回调Bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-26
 */
@RequiredArgsConstructor
@Component
public class OutBoundTaskSubmitBean<T> extends BaseCustomBeanRun<T> {


    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;
    private final CommonOutboundUtil commonOutboundUtil;

    /**
     * 业务逻辑 出库任务提交
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 获取请求参数
        baseFlowDto.translateClass(JSONObject.class);
        JSONObject jsonVO = (JSONObject) baseFlowDto.getData();
        String taskId = jsonVO.getString("id");
        if (StrUtil.isEmpty(taskId)) {
            //直接提交
            OutBoundTaskSaveVO saveVO = jsonVO.toJavaObject(OutBoundTaskSaveVO.class);
            WmsOutReceiptTask wmsOutReceiptTask = new WmsOutReceiptTask();
            BeanUtils.copyProperties(saveVO, wmsOutReceiptTask);
            taskId = wmsOutReceiptTaskService.saveMain(wmsOutReceiptTask, saveVO.getWmsOutReceiptTaskMaterialList());
            wmsOutReceiptTaskService.update(new LambdaUpdateWrapper<WmsOutReceiptTask>()
                    .set(WmsOutReceiptTask::getDocumentStatus, DocumentStatusEnum.COMMIT.getCode())
                    .eq(WmsOutReceiptTask::getId, taskId)
            );
        } else {
            WmsOutReceiptTask outReceiptTask = wmsOutReceiptTaskService.getById(taskId);
            if (Objects.isNull(outReceiptTask)) {
                throw new JeecgBootException("出库任务不存在");
            }
            if (DocumentStatusEnum.CLOSE.getCode().equals(outReceiptTask.getDocumentStatus())) {
                throw new JeecgBootException("提交失败，所属出库任务已关闭");
            }
            wmsOutReceiptTaskService.update(new LambdaUpdateWrapper<WmsOutReceiptTask>()
                    .set(WmsOutReceiptTask::getDocumentStatus, DocumentStatusEnum.COMMIT.getCode())
                    .eq(WmsOutReceiptTask::getId,taskId));
        }
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
    }
}
